package com.example.server.controller;

import com.example.server.pojo.MyEmployee;
import com.example.server.pojo.PageParam;
import com.example.server.pojo.RespBean;
import com.example.server.service.MyEmployeeService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@Slf4j
@RequestMapping("/api")
public class MyEmployeeCtl {
    @Autowired
    private MyEmployeeService myEmployeeService;

    @ApiOperation(value = "查询所有员工")
    @GetMapping("/allEmp")
    public RespBean allEmp(){
        List<MyEmployee> result = myEmployeeService.allEmp();
        log.info("-----allEmp结果："+result);

        if (result != null){
            return new RespBean(200,"success",result);
        }else {
            return new RespBean(402,"获取员工失败",null);
        }
    }

    @ApiOperation(value = "分页查询所有员工")
    @GetMapping("/allEmpPage")
    public RespBean allEmpPage(PageParam pageParam){
        Map result = myEmployeeService.allEmpPage(pageParam);
        log.info("-----allEmpPage结果："+result);

        if (result != null){
            return new RespBean(200,"success",result);
        }else {
            return new RespBean(402,"获取员工失败",null);
        }
    }

    @ApiOperation(value = "封装工具包分页查询员工")
    @GetMapping("/pageEmp")
    public RespBean pageEmp(PageParam pageParam){
        Map result = myEmployeeService.pageEmp(pageParam);
        log.info("-----allEmpPage结果："+result);

        if (result != null){
            return new RespBean(200,"success",result);
        }else {
            return new RespBean(402,"获取员工失败",null);
        }
    }

    @GetMapping("/allEmpfor")
    public RespBean querylikeEmp(@RequestParam(name = "name") String name){
        List<MyEmployee> result = myEmployeeService.querylikeEmp(name);
        log.info("-----querylikeEmp结果："+result);

        if (result != null){
            return new RespBean(200,"success",result);
        }else {
            return new RespBean(402,"获取员工失败",null);
        }
    }

    @GetMapping("/allEmp/{id}")
    public RespBean selectOneEmp(@PathVariable("id") Long id){
        MyEmployee result = myEmployeeService.selectOneEmp(id);
        log.info("-----selectOneEmp结果："+result);

        if (result != null){
            return new RespBean(200,"success",result);
        }else {
            return new RespBean(402,"获取员工失败",null);
        }
    }

    @PostMapping("/addEmp")
    public RespBean addEmp(@RequestBody MyEmployee myEmployee){
        if (myEmployee.getId() == null){
//            UUID uuid = UUID.randomUUID();
//            myEmployee.setId(Integer.valueOf(String.valueOf(uuid)));
//            log.info("随机生成uuid");
            /**
             * 根据时间戳生成唯一id
             */
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
            String id = sdf.format(System.currentTimeMillis());
            long longid = Long.valueOf(id).longValue();
            myEmployee.setId(longid);
        }
        Integer num = myEmployeeService.addEmp(myEmployee);
        log.info("-----结果："+num);

        if (num != null){
            return new RespBean(200,"success","新增成功");
        }else {
            return new RespBean(402,"获取员工失败",null);
        }
    }

    @PutMapping("/updateEmp/{id}")
    public RespBean updateEmp(@PathVariable("id") Long id,
                              @RequestBody MyEmployee myEmployee){
        myEmployee.setId(id);
        Integer num = myEmployeeService.updateEmp(myEmployee);
        log.info("-----结果："+num);

        if (num != null){
            return new RespBean(200,"success","修改成功");
        }else {
            return new RespBean(402,"获取员工失败",null);
        }
    }

    @DeleteMapping("/delEmp/{id}")
    public RespBean delEmp(@PathVariable("id") Long id){
        Integer num = myEmployeeService.delEmp(id);
        log.info("-----结果："+num);

        if (num != null){
            return new RespBean(200,"success","新增成功");
        }else {
            return new RespBean(402,"获取员工失败",null);
        }
    }
}
